<?php
/**
 * Created By Project
 * Author: 陈东
 * Copyright：陈东
 * Source Code：陈东
 * Explain：I shall not authorize for any other commercial purposes, law breakers must be prosecuted.
 */
namespace Home\Controller;
use Think\Controller;
class WfilmController extends Controller {


	/**
	 * 手机,PC端区分
	 */
	public function index(){
		$wfilm = D(C('TABLE_WFILM'));
		$res = $wfilm->where(array('state' => 1))->order('id desc')->select();
		$this->assign('wfilmlist', $res);
		$this->assign('wfilmtype', $this->get_wfilmtype());
		if(ismobile()){
			$this->display('Wfilm/mobile/index');
		}else{
			$this->display();
		}
	}

	/**
	 * 列表页
	 */
	public function listvote($typeid = null){
		$wfilm = D(C('TABLE_WFILM'));
		$res = $wfilm->where(array('type_id' => $typeid, 'state' => 1))->order('wfilm_votenum desc')->select();
		$this->assign('wfilmlist', $res);
		$wfilmtype = $this->get_wfilmtype();
		$this->assign('wfilmtype', $wfilmtype);
		if(ismobile()){
			$this->islogin(getURL());
			$this->display('Wfilm/mobile/list');
		}else{
			foreach($wfilmtype as $v){
				if($v['id'] == $typeid){
					$wfiletype_name = $v['typename'];
				}
			}
			$this->assign('wfilmtype_name', $wfiletype_name);
			$this->display();
		}
	}

	public function islogin($getURL){
		if(empty($_COOKIE['user_userinfo'])){
			header("Location:".C('LOGINADDRES').$getURL);
			exit;
		}
	}

	/**
	 * 获取微电影大赛分类
	 */
	protected function get_wfilmtype(){
		$wfilmtype = D(C('TABLE_WFILMtype'));
		$where['id'] = array('neq', 12);
		$res = $wfilmtype->where($where)->order('id asc')->select();
		if($res){
			foreach($res as &$v){
				$v['subinfo'] = $this->get_wfilmlist_info($v['id']);
			}

		}
		return $res;
	}

	/**
	 * 根据TYPEID获取列表信息
	 */
	protected function get_wfilmlist_info($type_id = null){
		$wfilm = D(C('TABLE_WFILM'));
		$res = $wfilm->where(array('type_id' => $type_id, 'state' => 1))->order('wfilm_votenum DESC')->limit(6)->select();
		return $res;
	}

	/**
	 * 生成二维码
	 */
	public function sccode(){
		$typeid = I('post.typeid');
		$resid = I('post.resid');
		echo $this->qrcode($typeid, $resid);
	}

	public function qrcode($typeid, $resid){
		vendor("phpqrcode.phpqrcode");
		$object = new \QRcode();
		$level=3;
		$size=4;
		//容错级别
		$errorCorrectionLevel =intval($level);
		//生成图片大小
		$matrixPointSize = intval($size);
		$path = "assets/codeimgs/";
		$fileName = $path.$typeid.$resid.'.png';
		$data = C("WEBSITE").U('article', array('typeid' => $typeid, 'resid' => $resid));
		if(!$this->checkimg($fileName)){
			$object->png($data, $fileName,  $errorCorrectionLevel, $matrixPointSize, 2);
		}
		return C("WEBSITE").'/'.$fileName;
	}

	/**
	 * 详情页
	 */
	public function article($typeid, $resid){
		$wfilm = D(C('TABLE_WFILM'));
		$this->assign('atcdetail', $wfilm->find($resid));
		if(ismobile()){
			$this->islogin(getURL());
			$this->display('Wfilm/mobile/details');
		}else{
			$this->display();
		}
	}

	/**
	 * 判断二维码是否存在
	 */
	protected function checkimg($imgurl){
		$res = false;
		if(file_exists($imgurl)){
			$res = true;
		}
		return $res;
	}

	/**
	 * 添加投票数
	 */
	public function addVote(){
		$this->islogin(getURL());
		$get_userinfo 	= $_COOKIE['user_userinfo'];
		$articid = $_POST['articid'];
		$r = $this->vote_card($get_userinfo, $articid);
		if($r == 1){
			$wfilm = D(C('TABLE_WFILM'));
			$where["id"] = $articid;
			$result = $wfilm->where($where)->setInc('wfilm_votenum');
			if($result) $res = array('r' => 1);
		}else{
			$res = array('r' => $r);
		}
		echo json_encode($res);
	}
	/**
	 * 记录微电影投票记录
	 */
	protected function vote_card($get_userinfo, $type_id = null){
		$count = $this->check_vote($get_userinfo);
		if($count > 2){
			// 当天投票超过次数
			return -1;
		}else{
			$wcard = D(C('TABLE_WFILMCARD'));
			$wcard->type_id = $type_id;
			$wcard->openid = $get_userinfo;
			$wcard->c_date = date("Y-m-d");
			$res = $wcard->add();
			if($res){
				// 记录成功
				return 1;
			}else{
				// 记录失败
				return 0;
			}
		}

	}
	/**
	 * 检查今天是否已经超过投票数
	 */
	protected function check_vote($get_userinfo){
		$wcard = D(C('TABLE_WFILMCARD'));
		$cur_date = date('Y-m-d');
		$count = $wcard->where("c_date >= '{$cur_date}' and openid='$get_userinfo'")->count();
		return $count;
	}


}